<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="!isActive"><slot name="item-icon"></slot></div>
    <div v-else><slot name="item-icon-active"></slot></div>
    <div :style="activeStyle"><slot name="item-text"></slot></div>
  </div>
</template>

<script>
export default {
  name: 'TabBarItem',
  props: { // 动态获取路由path  父传子
    path: String,
    activeColor: {
      type: String,
      default: 'red'
    }
  },
  data () {
    return {
      // isActive: true
    }
  },
  computed: { // 计算属性
    isActive () {
      // 动态判断 indexOf==-1标识没有找到相同的
      return this.$route.path.indexOf(this.path) !== -1
    },
    activeStyle () { // v-bind动态绑定属性 ，并把属性抽离到计算属性中
      return this.isActive ? {color: this.activeColor} : {}
    }
  },
  methods: {
    itemClick () {
      console.log('itemClick')
      this.$router.push('/location').catch(err => { console.log(err) }) // 捕获连续点击同一个按钮报错的异常
      this.$router.push(this.path)
    }
  }
}
</script>

<style>
  .tab-bar-item {
    flex: 1; /* 均等分 */
    text-align: center;
    height: 49px;
    font-size: 14px;
  }

  .tab-bar-item img{
    width: 24px;
    height: 24px;
    margin-top: 3px;
    vertical-align: middle; /* 去除图片垂直边距 */
  }
</style>
